package com.definesys.smecrailtransit.adapter.SelectMonthAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.api.SmecRailApi;
import com.definesys.smecrailtransit.module.MonthForm.MonthFormFragment;
import com.definesys.smecrailtransit.module.SiteDetails.SiteDetailsFragment;
import com.definesys.smecrailtransit.module.TotalTime.TotalTimeFragment;
import com.definesys.smecrailtransit.utils.CommonUtils;
import com.definesys.smecrailtransit.utils.ToastyUtils;
import com.hwangjr.rxbus.Bus;


import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by cjy on 2017/7/27.
 */

public class SelectMonthAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {
    private ArrayList<String> list = new ArrayList<>();
    public String selectMonth;
    public String nowMonth;
    private Context context;
    protected Bus mRxbus;
    public int click=-1;
    private String type;

    public SelectMonthAdapter(String selectMonth,String nowMonth, Context context,Bus mRxbus,String type){
        this.selectMonth = selectMonth;
        for(int i = 0; i< 12;i++){
            this.list.add(String.valueOf(i+1));
        }

        this.nowMonth = nowMonth;
        this.context = context;
        this.mRxbus= mRxbus;
        this.type = type;
    }

    public void setClick(int click) {
        this.click = click;
    }

    public void setType(String type){
        this.type = type;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        click=position;
        mRxbus.post(MonthFormFragment.CHANGE_MONTH,SelectMonthAdapter.this.list.get(position));
        notifyDataSetChanged();
    }

    public final class ViewHolder {
        public TextView text;
        public LinearLayout item;
        public LinearLayout border;
    }


    @Override
    public int getCount() {
        return this.list.size();
    }

    @Override
    public Object getItem(int position) {
        return this.list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
//        if (convertView == null) {

        viewHolder = new ViewHolder();
        // 获得容器
        if(Integer.valueOf(list.get(position)) > Integer.valueOf(this.nowMonth) || Integer.valueOf(list.get(position)) < 7){
            convertView = LayoutInflater.from(this.context).inflate(R.layout.layout_grey_month_item, null);
        }else if(list.get(position).equals(this.selectMonth)) {
            convertView = LayoutInflater.from(this.context).inflate(R.layout.layout_select_month_item, null);
        }else{
            convertView = LayoutInflater.from(this.context).inflate(R.layout.layout_unselect_month_item, null);
        }

        // 初始化组件
        //convertView = LayoutInflater.from(this.context).inflate(R.layout.layout_unselect_month_item, null);
        viewHolder.text = (TextView) convertView.findViewById(R.id.text);
        viewHolder.item = (LinearLayout) convertView.findViewById(R.id.item);

        if(click ==position){
            viewHolder.item.setBackgroundResource(R.drawable.month_radius_select);
        }

        convertView.setTag(viewHolder);


        viewHolder.text.setText(String.valueOf(this.list.get(position))+"月");

        viewHolder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // setClick(position);
                if(Integer.valueOf(list.get(position)) > Integer.valueOf(SelectMonthAdapter.this.nowMonth) || Integer.valueOf(list.get(position)) < 7){
                    ToastyUtils.showWaringToast(SelectMonthAdapter.this.context,""+list.get(position)+"月份暂无数据");
                }else{
                    if(SelectMonthAdapter.this.type.equals(SmecRailApi.MonthCode.MONTHFORM)){
                        mRxbus.post(MonthFormFragment.CHANGE_MONTH,SelectMonthAdapter.this.list.get(position));
                    }
                    if(SelectMonthAdapter.this.type.equals(SmecRailApi.MonthCode.TOTALTIME)){
                        mRxbus.post(TotalTimeFragment.CHANGE_MONTH,SelectMonthAdapter.this.list.get(position));
                    }
                    if(SelectMonthAdapter.this.type.equals(SmecRailApi.MonthCode.STATUSFORM) ||
                            SelectMonthAdapter.this.type.equals(SmecRailApi.MonthCode.STATUSTIME)){
                        HashMap<String,String> hashMap = new HashMap<String, String>();
                        hashMap.put("type",type);
                        hashMap.put("month",list.get(position));
                        mRxbus.post(SiteDetailsFragment.CHANGE_MONTH,hashMap);
                    }
                }
            }
        });

        return convertView;
    }
}
